<?
//BaseAction里面做了一些验证的操作
class RequireAdminAction extends BaseAction {
  private $_types = array(
    1=>array(name=>'活动',sub=>array(1=>'会务',2=>'聚会')),
    3=>array(name=>'培训',sub=>array(0=>'')),
    5=>array(name=>'招聘求职',sub=>array(1=>'招聘',2=>'求职')),
    7=>array(name=>'EHS需求',sub=>array(1=>'产品',2=>'EHS服务',3=>'工程',4=>'悬赏或招标'))
  );
  public function index() {
    $m = M('ARequire');
    import("@.ORG.Page"); //导入分页类
    $records = 20;//每页多少条记录
	//dump($_SESSION);
    //下面这些可以整理成数组的，不过怕写得太简洁自己到时都难看的明白,顺路初始化搜索的一些条件
    $where = '1';
    if ($_GET['from'] && $_GET['from'] != '') {
      $where .= ' and pub_time > "'.$_GET['from'].'"';
      $this->assign('from', $_GET['from']);
    }
    if ($_GET['to'] && $_GET['to'] != '') {
      $where .= ' and pub_time < "'.$_GET['to'].'"';
      $this->assign('to', $_GET['to']);
    }
    if ($_GET['type'] > 0) {
      $where .= ' and type = '.$_GET['type'];
      $this->assign('type', $_GET['type']);
    }
    if ($_GET['sub'] > 0) {
      $where .= ' and sub = '.$_GET['sub'];
      $this->assign('sub', $_GET['sub']);
    }
    //地区暂时隐藏
    if ($_GET['dq_id'] > 0) {
      $where .= ' and dq_id = '.$_GET['dq_id'];
    }
    if ($_GET['title'] && $_GET['title'] != '') {
      $where .= ' and title like "%'.$_GET['title'].'%"';
      $this->assign('title', $_GET['title']);
    }
    $count = $m->where($where)->count();
    $p = new Page ($count,$records);
    $p->setConfig('header','篇记录');
    $p->setConfig('prev',"上一页");
    $p->setConfig('next','下一页');
    $p->setConfig('first','<<');
    $p->setConfig('last','>>');
    $page = $p->show ();
    $list = $m->where($where)->order('id desc')->limit($p->firstRow.",".$p->listRows)->select();
    $this->assign("count", $count);
    $this->assign("list", $list);
    $this->assign("nowPage",$p->nowPage);
    $this->assign("page", $page );
    $this->assign("records",$records);
    $this->assign('types', $this->_types);
    //地区select初始化
    $list = $m->query('select * from a_region where did = 0');
    $this->assign('region', $list);
    $this->display();
  }

  //地区下拉框
  public function ajaxChgRegion() {
    $id = $_POST['id'];
    $m = M();
    $sql = 'select * from a_region where did = '.$id;
    $list = $m->query($sql);
    $this->ajaxReturn($list);
  }

  //1-置顶，2-取消置顶，3-审核通过，4-审核不通过，5-删除
  public function operateSubmit() {
    $type = $_POST['type'];
    $ids = implode(',', $_POST['ids']);
    $m = M();
    //目前还没有评论，迟点要把评论也删掉，等到做评论的时候再整理这个数组
    $sql = array(
      1=>'update a_require set top = 1 where id in ('.$ids.')',
      2=>'update a_require set top = 0 where id in ('.$ids.')',
      3=>'update a_require set status = 1 where id in ('.$ids.')',
      4=>'update a_require set status = 9 where id in ('.$ids.')',
      5=>'delete from a_require where id in ('.$ids.')',
    );
    $result = $m->query($sql[$type]);
    if ($result !== false){
	  if($type==5)
	     @$this->_delAttach($ids);
	  $this->success("更改成功");
	}
    else $this->error('更改失败');
  }

  //具体内容页面
  public function content() {
    $m = M('ARequire');
    $content = $m->where('id='.$_GET['id'])->find();
    $region = $this->_getRegionByID($content['dq_id']);
    $this->assign('region', $region);
    $this->assign('content', $content);
    $this->assign('types', $this->_types);
    $this->display();
  }

  //根据id，找到地区信息，格式如： 广东省/广州市
  private function _getRegionByID($id) {
    $m = M();
    $sql = 'select t1.id as fid,t1.title as ftitle,t2.id,t2.title
      from a_region t1
      left join a_region t2 on (t1.id = t2.did)
      where t2.id = '.$id;
    $result = $m->query($sql);
    return $result[0];
  }
  
  //删除 附件操作
  private function _delAttach($ids){
    if(is_array($ids))
	  $sql=" related_id in(".implode(',',$ids)." and type='require'";
	else
	  $sql=" related_id={$ids} and type='require'";
	 
	 $m=M('AAttachments');
	 $items=$m->where($sql)->select();
	 
	 //启动事务
	 $m->startTrans();
	 $flag=C('DEV_FLAG');
	 if($m->where($sql)->delete()){
	   foreach($items as $it){
	   //exit(C('DEV_FLAG'));
	   $flag==1?unlink("F:/www/main/".$it['file_path']):unlink("E:/www/main/".$it['file_path']);
	   //$flag==1?exit("F:/www/main/".$it['file_path']):exit("E:/www/main/".$it['file_path']);
	   //alert("F:/www/main/".$it['file_path']."\r\n".$m->getLastSQL());
	   }
	   $m->commit();
	 }else{
	   $m->rollback();
	 }
  }
  
  
}
?>